﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ActiveStory.IO;
using ActiveStory.IO.XmlElementNames;

namespace ActiveStory.DataManager
{
    public class ActiveStoryPageMetadata
    {
        #region Constructors
        public ActiveStoryPageMetadata(String pageName, int pageNumber, String pageFileLocation, String xamlFileLocation, String thumbnailFileLocation, String buttonToPageMapFileLocation)
        {
            _pageName = pageName;
            if (pageNumber < 0)
                throw new Exception("ActiveStoryPageMetadata.ActiveStoryPageMetadata(): Invalid page number specified.");
            _pageNumber = pageNumber;
            if (pageFileLocation.Equals(String.Empty) || !File.Exists(FileManager.GetAbsolutePath(pageFileLocation)))
                throw new FileNotFoundException("ActiveStoryPageMetadata.ActiveStoryPageMetadata(): Invalid page file location specified.");
            _pageFileLocation = pageFileLocation;
            if (xamlFileLocation.Equals(String.Empty) || !File.Exists(FileManager.GetAbsolutePath(xamlFileLocation)))
                throw new FileNotFoundException("ActiveStoryPageMetadata.ActiveStoryPageMetadata(): Invalid Xaml file location specified.");
            _xamlFileLocation = xamlFileLocation;
            if (thumbnailFileLocation.Equals(String.Empty) || !File.Exists(FileManager.GetAbsolutePath(thumbnailFileLocation)))
                throw new FileNotFoundException("ActiveStoryPageMetadata.ActiveStoryPageMetadata(): Invalid thumbnail file location specified.");
            _thumbnailFileLocation = thumbnailFileLocation;
            if (buttonToPageMapFileLocation.Equals(String.Empty) || !File.Exists(FileManager.GetAbsolutePath(buttonToPageMapFileLocation)))
                throw new FileNotFoundException("ActiveStoryPageMetadata.ActiveStoryPageMetadata(): Invalid buttonToPageMap file location specified.");
            _buttonToPageMapFileLocation = buttonToPageMapFileLocation;
        }
        #endregion
        #region Fields
        protected string _pageName;
        protected int _pageNumber = -1;
        protected string _thumbnailFileLocation;
        protected string _buttonToPageMapFileLocation;
        protected string _xamlFileLocation;
        protected string _pageFileLocation;
        #endregion
        #region Properties
        public String PageName
        {
            get
            {
                return _pageName;
            }
            set
            {
                _pageName = value;
            }
        }
        public int PageNumber
        {
            get
            {
                return _pageNumber;
            }
        }
        public int DisplayedPageNumber
        {
            get
            {
                return _pageNumber + 1;
            }
        }
        public String DisplayedLabel
        {
            get
            {
                return DisplayedPageNumber + ": " + PageName;
            }
        }
        public String ThumbnailFileLocation
        {
            get
            {
                return _thumbnailFileLocation;
            }
        }
        public String TempThumbnailPath
        {
            get
            {
                string tempfileName = Path.GetFileName(System.IO.Path.GetTempFileName());
                File.Copy(FileManager.GetAbsolutePath(_thumbnailFileLocation), Path.Combine(ActiveStoryApplication.TempFolder, tempfileName));
                return Path.Combine(ActiveStoryApplication.TempFolder, tempfileName);
            }
        }
        public String ButtonToPageMapFileLocation
        {
            get
            {
                return _buttonToPageMapFileLocation;
            }
        }
        public String XamlFileLocation
        {
            get
            {
                return _xamlFileLocation;
            }
        }
        public String PageFileLocation
        {
            get
            {
                return _pageFileLocation;
            }
        }
        #endregion
        #region Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectName"></param>
        /// <param name="pageFileName">Must be passed through FileManager.CreatePageFileName()</param>
        /// <param name="pageObject"></param>
        /// <param name="xamlFileLocation"></param>
        /// <param name="thumbnailFileLocation"></param>
        /// <returns></returns>
        public static ActiveStoryPageMetadata SavePage(String projectName, String pageFileName, ActiveStoryPage pageObject, String xamlFileLocation, String thumbnailFileLocation)
        {
            IActiveStoryPageWriter aspw = new XmlActiveStoryPageWriter();
            IMapFileWriter<int,int> mfw=new XmlMapFileWriter<int,int>();
            mfw.SaveFile(FileManager.GetAbsolutePath(FileManager.CreateButtonMapFileName(projectName,pageObject.PageNumber)),pageObject.ButtonToPageMap,
                ButtonFileXmlNames.RootElement,ButtonFileXmlNames.ItemElement,
                ButtonFileXmlNames.ButtonNumberElement,ButtonFileXmlNames.PageNumberElement);
            
            aspw.SaveFile(FileManager.GetAbsolutePath(pageFileName), pageObject.PageName,pageObject.PageNumber,thumbnailFileLocation,xamlFileLocation, FileManager.CreateButtonMapFileName(projectName,pageObject.PageNumber),pageObject.ButtonCounter);
            return new ActiveStoryPageMetadata(pageObject.PageName, pageObject.PageNumber, pageFileName, xamlFileLocation, thumbnailFileLocation, FileManager.CreateButtonMapFileName(projectName, pageObject.PageNumber));
        }
        public static ActiveStoryPage LoadPage(ActiveStoryPageMetadata pageMetadata)
        {
            IActiveStoryPageReader aspr = new XmlActiveStoryPageReader();
            String[] metadata = aspr.ParseFile(FileManager.GetAbsolutePath(pageMetadata.PageFileLocation));
            IMapFileReader<int,int> mfr=new XmlMapFileReader<int,int>();
            Dictionary<int,int> map= (Dictionary<int,int>)mfr.ParseFile(FileManager.GetAbsolutePath(metadata[5]),ButtonFileXmlNames.RootElement,ButtonFileXmlNames.ItemElement,
                ButtonFileXmlNames.ButtonNumberElement,ButtonFileXmlNames.PageNumberElement);
            ActiveStoryPage result = new ActiveStoryPage(metadata[0], int.Parse(metadata[1]), int.Parse(metadata[2]),metadata[3], metadata[4], map);
            return result;
        }
        public static ActiveStoryPageMetadata LoadMetadata(String pageFileName)
        {
            IActiveStoryPageReader aspr = new XmlActiveStoryPageReader();
            String[] metadata = aspr.ParseFile(FileManager.GetAbsolutePath(pageFileName));
            return new ActiveStoryPageMetadata(metadata[0], int.Parse(metadata[1]), pageFileName, metadata[3], metadata[4], metadata[5]);
        }
        #endregion
    }
}
